#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

pthread_rwlock_t rwlock;


//无名管道，多管道写

int main()
{
	int pfd[2];
	int re;
	int i;
	char buf[64] = {0};
	pid_t pid;

	//创建无名管道
	re = pipe(pfd);
	if(re < 0)
	{
		perror("pipe");

	}

	//创建两个进程写
	for(i = 0; i < 2; i++)
	{
		pid = fork();
		if(pid < 0)
		{
			perror("fork");
		}else if(pid == 0)
		{
			break;

		}else
		{
			//父进程不做处理
		}


	}


	//读文件和写
	if(i == 0)
	{
		close(pfd[0]);
		while(1)
		{
			strcpy(buf, "child 1 hello word");
			pthread_rwlock_wrlock(&rwlock);
			write(pfd[1], buf, 64);
			pthread_rwlock_unlock(&rwlock);
			sleep(1);
		}

	}

	if(i == 1)
	{
		close(pfd[0]);
		while(1)
		{
			strcpy(buf, "child 2 hello xiaoyang");
			pthread_rwlock_wrlock(&rwlock);
			write(pfd[1], buf, 64);
			pthread_rwlock_unlock(&rwlock);
			sleep(1);

		}

	}

	if(i == 2)
	{

		close(pfd[1]);
		while(1)
		{
			memset(buf, 0, 40);
			pthread_rwlock_rdlock(&rwlock);
			int ret = read(pfd[0], buf, 64);
			if(ret < 0)
			{
				perror("read");
			}
			printf("read: %s\n", buf);
			pthread_rwlock_unlock(&rwlock);
			sleep(1);

		}


	}




























}







